package com.voice.call.common;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.context.MessageSource;

import com.voice.call.utils.SpringContextUtil;
import com.voice.call.utils.StringUtil;

public class ConstLang {
	
	//public static final List<String> langArr = Arrays.asList("en","fr","de","ar"); 
	public static final List<String> langArr = Arrays.asList("en","fr","de"); 
			
	private String code; //多语言code
	private Locale locale; //多语言
	private String msg;//信息
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Locale getLocale() {
		return locale;
	}
	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public ConstLang() {
		this.locale = Locale.ENGLISH;
		this.code = "SUCCESS";
		this.msg = "success";
	}
	
	public ConstLang(String code,String lang) {
		if (!StringUtil.isEmpty(lang)&&lang.equals("en")) { //英语
			this.locale = Locale.ENGLISH;
		}else if (!StringUtil.isEmpty(lang)&&lang.equals("fr")) { //法语
			this.locale = Locale.FRENCH;
		}else if (!StringUtil.isEmpty(lang)&&lang.equals("de")) { //德语
			this.locale = Locale.GERMAN;
		}else if (!StringUtil.isEmpty(lang)&&lang.equals("ar")) { //阿拉伯语
			this.locale = new Locale("ar","Saudi Arabia");
		}else {
			this.locale = Locale.ENGLISH;
		}
		this.code = code;
		
		try {
			
			if (!StringUtil.isEmpty(this.code)) {
				MessageSource messageSource = (MessageSource)SpringContextUtil.getBean("messageSource");
				this.msg = messageSource.getMessage(this.getCode(), new Object[]{}, this.locale);
			}else {
				this.msg = "";
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			this.msg = "";
		}
	}
	
	public static String getMessage(String code,String lang) {
		return new ConstLang(code, lang).getMsg();
	}
	
	public static String getLang(Map<String, Object> params) {
		
		String lang = Const.LANGUAGE;
		if (params!=null && params.get("lang")!=null) {
			lang = params.get("lang").toString().trim();
			if (!langArr.contains(lang)) {
				lang = Const.LANGUAGE;
			}
		}
		return lang;
	}
}
